<?php
/**
 * Created by PhpStorm.
 * User: pc
 * Date: 2019/7/29
 * Time: 23:17
 */

namespace app\admin\model;


use think\Db;

class AttendanceDetail extends Base
{
    public function getList($time)
    {
        //获取所有部门
        $department = [];
        $department1 = Db::table('zm_wechat_department')->where(['parentid' => 1])->order('`order` desc')->select();
        foreach ($department1 as $key1 => $value1) {
            $department[] = $value1;
            $department2 = Db::table('zm_wechat_department')->where(['parentid' => $value1['id']])->order('`order` desc')->select();
            if($department2) {
                foreach ($department2 as $key2 => $value2) {
                    $department[] = $value2;
                    $department3 = Db::table('zm_wechat_department')->where(['parentid' => $value2['id']])->order('`order` desc')->select();
                    if($department3) {
                        foreach ($department3 as $k => $v) {
                            $department[] = $v;
                        }
                    }
                }
            }
        }
        //获取所有用户
        $list = [];
        foreach ($department as $k => $v) {
            $users =  WechatUser::where('department',$v['id'])->field('userid,name')->order('`order` desc')->select();
            foreach ($users as $u) {
                if(!empty($u)) {
                    $user = [
                        'pid' => '',
                        'departmentname' => $v['name'],
                        'username' => $u['name'],
                        'userid' => $u['userid'],
                        'status' => 0
                    ];
                    $map = [
                        'userid' => $u['userid'],
                        'attendance_time' => $time
                    ];
                    $res = $this->where($map)->find();
                    if($res) {
                        $user['attendance_time'] = $res['attendance_time'];
                        $user['status'] = $res['status'];
                        $user['pid'] = $res['pid'];
                        $attendance = Attendance::where('id',$res['pid'])->find();
                        $user['report_user'] = $attendance['username'];
                        $user['report_time'] = date("Y-m-d H:i",$attendance['create_time']);
                    }else {
                        $user['attendance_time'] = "";
                        $user['report_user'] = "";
                        $user['report_time'] = "";
                    }
                    $data = [0 => "休息", 1 => "出勤", 2=> "公出"];
                    $user['status_text'] = $data[$user['status']];
                    $list[] = $user;
                }
            }
        }
        return $list;
    }
}